const { executeAsModal } = require("photoshop").core;
const { batchPlay } = require("photoshop").action;
const { localFileSystem: fs } = require("uxp").storage;
// const ps = require('photoshop');
// const app = ps.app;
// const doc = app.activeDocument;
// const lay = doc.activeLayers[0];
//  export const Demos = () => {
//     return (
//         <>
//             <ColorPicker />
//         </>
//     )
// }

//
const kersAPP = function () {
    return kersAPP;
}
kersAPP.lib = {}//库
kersAPP.layer = {};//图层
kersAPP.doc = {};//文档

/**
 * 模态执行函数
 * @param {*} fun （必需）： Photoshop 进入模态状态后要执行的 JavaScript 函数
 * @param {*} commandName 描述命令的字符串。此字符串显示在进度栏 UI 中
 */
kersAPP.lib.runModalFunction = async function (fun, commandName) {
    await executeAsModal(fun, { "commandName": commandName });
}

/**
 * DOM - 文本图层
 *       1.获取内容 2.修改内容
 *       contents
 */
Object.defineProperty(require('photoshop').app.Layer.prototype,
    "contents",
    {
        get: () => {
            // const result = 
            const result = batchPlay(
                [
                    {
                        _obj: "get",
                        _target: [
                            {
                                _property: "textKey"
                            },
                            {
                                _ref: "layer",
                                _enum: "ordinal",
                                _value: "targetEnum"
                            }

                        ],
                        _options: {
                            dialogOptions: "dontDisplay"
                        }

                    }
                ], {
                synchronousExecution: true,
                modalBehavior: "fail"
            });
            return result[0].textKey.textKey;
        },
        set: (e) => {

            batchPlay(
                [
                    {
                        _obj: "set",
                        _target: [
                            {
                                _ref: "textLayer",
                                _enum: "ordinal",
                                _value: "targetEnum"
                            }
                        ],
                        to: {
                            _obj: "textLayer",
                            textKey: e,

                        },
                        _options: {
                            dialogOptions: "dontDisplay"
                        }

                    }
                ], {
                synchronousExecution: true,
                modalBehavior: "fail"
            });
        }
    })
Object.defineProperty(require('photoshop').app.Photoshop.prototype,
    "modalStateChanged", {
    get: () => {
        const result = batchPlay(
            [
                {
                    _obj: "modalStateChanged",
                    level: 1,
                    state: {
                        _enum: "state",
                        _value: "enter"
                    },
                    kcanDispatchWhileModal: true,
                    title: "Adobe Photoshop",
                    _options: {
                        dialogOptions: "dontDisplay"
                    }
                }
            ], {
            synchronousExecution: false,
            modalBehavior: "fail"
        });
        return result[0];

    }
})
/**
 * DOM - 智能对象图层
 *       1.路径获取 2.路径特换
 *       smartObjectReplace
 */
Object.defineProperty(require('photoshop').app.Layer.prototype,
    "smartObjectReplace",
    {
        get: () => {


            const result = batchPlay(
                [
                    {
                        _obj: "get",
                        _target: [
                            {
                                _property: "smartObject"
                            },
                            {
                                _ref: "layer",
                                _enum: "ordinal",
                                _value: "targetEnum"
                            }
                        ],
                        _options: {
                            dialogOptions: "dontDisplay"
                        }
                    }
                ], {
                synchronousExecution: true,
                modalBehavior: "fail"
            });

            return result[0].smartObject.link._path//.link;



        },
        set: (e) => {

            async function tokenify(url) {
                return fs.createSessionToken(await fs.getEntryWithUrl("file:" + url));
            }
            async function actionCommands() {

                const result = await batchPlay(
                    [
                        {
                            _obj: "placedLayerReplaceContents",
                            null: {
                                _path: await tokenify(e),
                                _kind: "local"
                            },
                            _options: {
                                dialogOptions: "dontDisplay"
                            }
                        }
                    ],
                    {}
                );
            }

            runModalFunction(actionCommands, "Replace SmartObject Path");
            //return e;
        }

    }
)
Object.defineProperty(require('photoshop').app.Layer.prototype,
    "smartObjectEdit", {
    get: () => {

        async function actionCommands() {

            const result = await batchPlay(
                [
                    {
                        _obj: "placedLayerEditContents",
                        _options: {
                            dialogOptions: "dontDisplay"
                        }
                    }
                ],
                {}
            );

        }
        runModalFunction(actionCommands, "Edit SmartObject");

    }

})
//===========================


//const selectLayer = function (id) {
kersAPP.layer.selectLayer = function (id) {

    async function actionCommands() {

        const result = await batchPlay(
            [
                {
                    _obj: "select",
                    _target: [
                        {
                            _ref: "layer",
                            _id: id
                        }
                    ],
                    _options: {
                        dialogOptions: "dontDisplay"
                    }
                }
            ],
            {}
        );
    }

    // async function runModalFunction() {
    //    await executeAsModal(actionCommands, { "commandName": "Action Commands" });
    // }

    kersAPP.lib.runModalFunction(actionCommands, "Edit SmartObject");
}
